self.Module = {
    onRuntimeInitialized: function () {
        onWasmLoaded();
    }
};

importScripts('./libdinadecoder.js')
importScripts('./comm.js')


function Decode() {
    this.wasmLoaded = false;
    this.decode_instance = null; // 解码器实例
    this.videoCallback = null; // 获取视频数据后回调
    this.audioCallback = null; // 获取音频数据后回调

    this.cacheBuffer = null; // 内存对象

    this.dtype = null; // 数据类型
}

Decode.prototype.onWasmLoaded = function () {
    this.wasmLoaded = true;


    this.videoCallback = Module.addFunction(function (buff, size, intW, intH, pts, dts) {
        // 获取到了数据
        let outArray = Module.HEAPU8.subarray(buff, buff + size);
        let data = new Uint8ClampedArray(outArray); //Uint8Array
        let frame = {
            state: DecodeStateSucc,
            data: data,
            width: intW,
            height: intH
        }
        self.postMessage(frame, [frame.data.buffer])

        data = null;
    }, 'viiiiii')

    this.audioCallback = Module.addFunction(function (buf, size, pts, dts) {
        // 获取到了数据
        console.error('audioCallback获取到了数据');
    }, 'viiii')



    self.postMessage({
        state: WasmLoaded
    })
}

Decode.prototype.initDecode = function (data) {
    let that = this;

    if (!this.wasmLoaded) {
        return
    }

    that.dtype = data.dtype

    that.cacheBuffer = Module._malloc(50 * 1024);
    console.error('内存已分配')

    // 初始化解码器
    if (data.type == 1) {
        console.error('解码器初始化 264')
        this.decode_instance = Module._initH264Decoder(that.videoCallback, that.audioCallback, outImgFrameType);
        self.postMessage({
            state: DecodeStateInitSucc
        })

    } else {
        console.error('解码器初始化 265')
        this.decode_instance = Module._initH265Decoder(that.videoCallback, that.audioCallback, outImgFrameType);
        self.postMessage({
            state: DecodeStateInitSucc
        })
    }

}

Decode.prototype.postDecode = function (data) {
    
    // 裸流
    // 将消息推送到解码器
    if (!this.wasmLoaded && this.decode_instance == null) {
        return
    }
    let that = this;

    let typedArray = new Uint8Array(data.data);
    Module.HEAPU8.set(typedArray, that.cacheBuffer);
    if (this.dtype == 'RAW') {
        // console.error('RAW')
        Module._addRawVideoFrameData(that.decode_instance, that.cacheBuffer, data.size, 0, 0);
    }
    if (this.dtype == 'RTP') {
        // console.error('RTP')
        Module._addRtpFrameData(that.decode_instance, that.cacheBuffer, data.size);
    }
    typedArray = null;
}


Decode.prototype.closeDecode = function () {
    // 关闭解码器  释放内存
    let that = this;
    Module._free(that.cacheBuffer)

    Module._closeDecoder(that.decode_instance)
    that.decode_instance = null;
}

self.decode = new Decode;

self.onmessage = function (msg) {
    // decode 线程
    let data = msg.data
    switch (data.state) {
        case SocketStateInitSucc:
            // 初始化解码器
            self.decode.initDecode(data)
            break;
        case SocketStateSucc:
            // 编码器数据传输
            self.decode.postDecode(data)
            break;
        case DecodeStateClose:
            self.decode.closeDecode()
            break;
    }
}

function onWasmLoaded() {
    if (self.decode) {
        self.decode.onWasmLoaded();
    } else {
        console.log("解码器报错!");
    }
}